//
//  LSZPopoverViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/5/16.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZPopoverViewController.h"
#import "LSZInformationChooseTableViewCell.h"
#import "LSZFavoritesListManager.h"
#import "LSZInformationChooseTableViewCell.h"

@interface LSZPopoverViewController ()
@property(nonatomic,strong) UITableView * tableViewList;
@property(nonatomic,strong) UIView *topView;
@property(nonatomic,strong) UIView *viewLowWhite;

@property(nonatomic,copy)NSArray * arrayImageView;
@property(nonatomic,copy)NSArray * arrayTitle;

@property(nonatomic,assign) NSInteger type;//布局用1有删除   2正常歌曲的 3收藏夹的

@property(nonatomic,strong) NSString *  musicId;//歌曲id
@property(nonatomic,strong) NSString * typeID;//专辑1 歌手2 歌曲3
@property(nonatomic,strong) NSString * fromWhichInterfaceVC;//从哪个界面过里的

@property(nonatomic,strong) NSString * chooseFavorites;//1
 
@property(nonatomic,strong) LSZFavoritesListManager * favoritesListManager;

@end

@implementation LSZPopoverViewController
- (void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark  - 从哪个界面过里的  过来界面的VC
 -(void)fromWhichInterface:(NSString *)string
{
    self.fromWhichInterfaceVC = string;
}
-(void)selectIndexRowWith:(NSInteger)value
{
    if (value == 1) {
        self.arrayImageView = @[@"弹窗-收藏",@"弹窗-分享",@"弹窗删除"];
        self.arrayTitle = @[@"收藏",@"分享",@"删除"];
    }else if (value == 2){
        self.arrayImageView = @[@"弹窗-收藏",@"弹窗-下载",@"弹窗-分享"];
        self.arrayTitle = @[@"收藏",@"下载",@"分享"];
    }else if (value == 3){//收藏夹列表
        [self getMusicNumber];
    }
    else if (value == 4){
        self.arrayImageView = @[@"弹窗-点赞",@"弹窗删除"];
        self.arrayTitle = @[@"点赞",@"删除"];
        [self queryWhetherTheDataThumbUp];
    }
    else if (value == 5){
        self.arrayImageView = @[@"创建新的文件夹",@"歌单管理"];
        self.arrayTitle = @[@"创建新的文件夹",@"歌单管理"];
    }else if (value == 6){
        self.arrayImageView = @[@"弹窗-下载",@"弹窗-分享"];
        self.arrayTitle = @[@"下载",@"分享"];
    }
    else{

    }
    self.type = value;
    [self.tableViewList reloadData];
}
#pragma mark - 歌曲的id
-(void)getMusicId:(NSString *)string
{
    self.musicId = string;
    if (self.type == 4) {
        [self queryWhetherTheDataThumbUp];
    }
}
#pragma mark - 1是专辑还是2歌手 3歌曲
- (void)distinguishType:(NSString *)string
{
    self.typeID = string;
    if (self.type  == 3) {
        [self getMusicNumber];
    }
    if (self.type == 4) {
        [self queryWhetherTheDataThumbUp];
    }}
#pragma mark - 查询数据是否点赞
-(void)queryWhetherTheDataThumbUp
{
    NSString * strType =  NullStringjudgment(NSStr(self.typeID));
    NSString * strMusicId =  NullStringjudgment(NSStr(self.musicId)) ;

    if (strType.length == 0 ||[strType isEqualToString:@""]) {
        return;
    }
    if (strMusicId.length == 0 ||[strMusicId isEqualToString:@""]) {
        return;
    }
    [LSZRequestData checkIfTheAudioIsThumbUp:self.typeID :self.musicId :^(BOOL isOK, NSString * string, NSDictionary * dic) {
        if (isOK == YES) {
            self.arrayImageView = @[@"点赞完",@"弹窗删除"];
            self.arrayTitle = @[@"取消点赞",@"删除"];
        }else{
            self.arrayImageView = @[@"弹窗-点赞",@"弹窗删除"];
            self.arrayTitle = @[@"点赞",@"删除"];
        }
        [self.tableViewList reloadData];
    }];
}
#pragma mark - 改变布局 子界面传1
-(void)ChooseTableViewFrame:(NSString *)string
{
    if ([string isEqualToString:@"1"]) {
        self.topView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.viewLowWhite.frame = CGRectMake(0,self.topView.frame.size.height-300, ScreenWidth,300);
    }
}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5];
        /*
         阴影区域点击
         */
        self.topView = [[UIView alloc]init];
        self.topView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.topView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.topView];

        [self.topView tapGesture:^(UIGestureRecognizer *ges) {
            [self hiddenMBhiddenView];
        }];

        self.viewLowWhite = [[UIView alloc]init];
        self.viewLowWhite.frame = CGRectMake(0,self.topView.frame.size.height-300, ScreenWidth,300);
        self.viewLowWhite.backgroundColor = [UIColor whiteColor];
        [self.viewLowWhite.layer setMasksToBounds:YES];
        [self.viewLowWhite.layer setCornerRadius:8];
        [self addSubview:self.viewLowWhite];

        UILabel * labelTitle = [[UILabel alloc]init];
        labelTitle.textColor = redUIColor;
        labelTitle.text = @"操作列表";
        labelTitle.font = FONT_18;
        labelTitle.textAlignment = NSTextAlignmentCenter;
        [self.viewLowWhite addSubview:labelTitle];
        if (ScreenWidth == 320) {
            //5
            labelTitle.frame = CGRectMake(0, 0, ScreenWidth, 45);
        }else if(ScreenWidth == 375){
            // 6 7
            labelTitle.frame = CGRectMake(0, 0, ScreenWidth, 50);
        }else {
            //plus
            labelTitle.frame = CGRectMake(0, 0, ScreenWidth, 54);
        }
        UIView * viewTop = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(labelTitle.frame), ScreenWidth, 0.3)];
        viewTop.backgroundColor = divider;//RGBColor(186, 186, 186)
        [self.viewLowWhite addSubview:viewTop];

        self.tableViewList = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(viewTop.frame), ScreenWidth,self.viewLowWhite.frame.size.height-CGRectGetMaxY(viewTop.frame)) style:UITableViewStyleGrouped];
        self.tableViewList.dataSource = self;
        self.tableViewList.delegate = self;
        self.tableViewList.separatorColor = divider;
        self.tableViewList.backgroundColor = [UIColor whiteColor];
        self.tableViewList.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        [self.tableViewList registerClass:[LSZInformationChooseTableViewCell class] forCellReuseIdentifier:@"LSZInformationChooseTableViewCell"];
        [self.viewLowWhite addSubview:self.tableViewList];
    }
    return self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ScreenWidth == 320) {
        //5
        return 45;
    }else if(ScreenWidth == 375){
        // 6 7
        return 50;
    }
    else{
        //plus
        return 58;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.chooseFavorites isEqualToString:@"1"]) {
        return  self.favoritesListManager.arrayData.count+1;
    }else{
        return self.arrayImageView.count;
     }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cell"];
    }
    return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //文件夹的名字
    if ([self.chooseFavorites isEqualToString:@"1"]) {
        if(indexPath.row == 0){
            LSZInformationChooseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZInformationChooseTableViewCell" forIndexPath:indexPath];
            cell.imageCollection.image = [UIImage imageNamed:@"创建新的文件夹"];
            cell.labelCollection.text = @"新建歌单";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }
            LSZFavoritesListMg  * mg = nil;
            mg = self.favoritesListManager.arrayData[indexPath.row-1];
            cell.textLabel.text = mg.name;
            [LSZSpecialPackaging setLabel_14_5_Size:cell.textLabel];
            cell.textLabel.textColor = titleColorLSZ;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }else{
        LSZInformationChooseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZInformationChooseTableViewCell" forIndexPath:indexPath];
        cell.imageCollection.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.arrayImageView[indexPath.row]]];
        cell.labelCollection.text = [NSString stringWithFormat:@"%@",self.arrayTitle[indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.chooseFavorites isEqualToString:@"1"]) {
        if(indexPath.row == 0){
            //新建歌单
            [self newSongList];
        }else{
#pragma mark - 选择的是歌单收藏夹的点击事件
            LSZFavoritesListMg  * mg = nil;
            mg = self.favoritesListManager.arrayData[indexPath.row-1];
            [self setCollectAdd:mg.id_id];
        }
    }else{
        NSString * strTitle = [NSString stringWithFormat:@"%@",self.arrayTitle[indexPath.row]];

        if ( [strTitle isEqualToString:@"收藏"]) {
            //收藏的
             [self getMusicNumber];
        }else if ([strTitle isEqualToString:@"删除"]){
            NSString * strDelete = [NSString stringWithFormat:@"%@DeleteLSZ",self.fromWhichInterfaceVC];
            NSNotification * notificationDelete =[NSNotification notificationWithName:strDelete object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notificationDelete];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:strDelete object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [self hiddenMBhiddenView];
        }else if ([strTitle isEqualToString:@"分享"]){
            NSNotification *notification =[NSNotification notificationWithName:self.fromWhichInterfaceVC object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:self.fromWhichInterfaceVC object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [self hiddenMBhiddenView];
        }
        else if ([strTitle isEqualToString:@"下载"]){
            NSString * strDownload = [NSString stringWithFormat:@"%@Download",self.fromWhichInterfaceVC];
            NSNotification *notification =[NSNotification notificationWithName:strDownload object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:strDownload object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [self hiddenMBhiddenView];
        } else if ([strTitle isEqualToString:@"点赞"]||[strTitle isEqualToString:@"取消点赞"]){
            NSString * strZan = [NSString stringWithFormat:@"%@Zan",self.fromWhichInterfaceVC];
            NSNotification *notificationZan =[NSNotification notificationWithName:strZan object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notificationZan];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:strZan object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [self hiddenMBhiddenView];
        }else if ([strTitle isEqualToString:@"创建新的文件夹"]){
            NSString * strFavorites = [NSString stringWithFormat:@"%@CreateNewFavorites",self.fromWhichInterfaceVC];
            NSNotification *notificationFavorites =[NSNotification notificationWithName:strFavorites object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notificationFavorites];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:strFavorites object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [self hiddenMBhiddenView];
        }else if ([strTitle isEqualToString:@"歌单管理"]){
            NSString * strPlaylist = [NSString stringWithFormat:@"%@PlaylistManagement",self.fromWhichInterfaceVC];
            NSNotification *notificationPlaylist =[NSNotification notificationWithName:strPlaylist object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notificationPlaylist];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:strPlaylist object:nil];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [self hiddenMBhiddenView];
        }
        else{

        }
    }
}
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
#pragma mark - 获取歌曲的文件夹
-(void)getMusicNumber
{
    NSString * typeString = NullStringjudgment(self.typeID);
    if (typeString.length == 0 || [typeString isEqualToString:@""]) {
        return;
    }
    if([[_UserM usercode] isEqualToString:@""]){
        NSLog(@"用户未登录,让用户先登录");
        NSString * stringLogin = [NSString stringWithFormat:@"%@Login",self.fromWhichInterfaceVC];
        NSNotification *notificationLogin =[NSNotification notificationWithName:stringLogin object:nil userInfo:nil];
        [[NSNotificationCenter defaultCenter] postNotification:notificationLogin];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:stringLogin object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [self hiddenMBhiddenView];
    }else{
        if ([self.typeID isEqualToString:@"1"]||[self.typeID isEqualToString:@"2"]) {
             //专辑和歌曲直接返回
            NSString * stringVC = [NSString stringWithFormat:@"%@CollectionAlbumSinger",self.fromWhichInterfaceVC];
            NSNotification *notificationCollection =[NSNotification notificationWithName:stringVC object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:notificationCollection];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:stringVC object:nil];
            [self hiddenMBhiddenView];
        }else{
            [LSZRequestResolutionDic getFavoritesList:self.typeID uid:@"" :^(BOOL isOk, NSString * string, LSZFavoritesListManager * manager) {
                if (isOk == YES) {
                    self.chooseFavorites = @"1";
                    self.favoritesListManager = manager;
                    [self.tableViewList reloadData];
                }
            }];
        }
    }
}
#pragma mark - 新建歌单
-(void)newSongList
{
    NSString * stringVC = [NSString stringWithFormat:@"%@newSongList",self.fromWhichInterfaceVC];
    NSNotification *newSongListCollection =[NSNotification notificationWithName:stringVC object:nil userInfo:nil];
    [[NSNotificationCenter defaultCenter] postNotification:newSongListCollection];
    [self hiddenMBhiddenView];
}
#pragma mark -已经选择文件夹了 歌曲收藏
-(void)setCollectAdd :(NSString *)Cat
{
    [LSZRequestData getSongCollection:Cat :self.musicId :^(BOOL isOk, NSString * string, NSDictionary * dic) {
        NSString * isOkString = [NSString stringWithFormat:@"%hhd",isOk];
        NSDictionary * dicString = [[NSDictionary alloc]initWithObjectsAndKeys:string,@"str",isOkString,@"ok", nil];
        NSString * stringVC = [NSString stringWithFormat:@"%@Collection",self.fromWhichInterfaceVC];
        NSNotification *notificationCollection =[NSNotification notificationWithName:stringVC object:nil userInfo:dicString];
        [[NSNotificationCenter defaultCenter] postNotification:notificationCollection];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:stringVC object:nil];
        [self hiddenMBhiddenView];
    }];
}
-(void)hiddenMBhiddenView
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeFromSuperview];
}
-(void)setStringAlter :(NSString *)str
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    hud.mode = MBProgressHUDModeText;//枚举类型不同的效果
    hud.label.text= [NSString stringWithFormat:@"%@",str];
    [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:2.0];
}
-(void)hiddenMBAction
{
    [MBProgressHUD hideHUDForView:self animated:YES];
}
@end
